angular.module('charity.banner',[])
    .controller('editHeadBannerCtrl', ['$scope', '$filter', 'bannerService', '$routeParams','logger',
    function ($scope, $filter, bannerService, $routeParams,logger) {

        $scope.headBannerSources = []
        bannerService.getHeadBannerSources().then(function(result){
            if(!result || result.code != 0 ){
                logger.logError((result && result.message) || '查询失败');
            }
            $scope.headBannerSources = result.data;

            $scope.headSources1 = $scope.headBannerSources[0] || {banner_url:''};
            $scope.headSources2 = $scope.headBannerSources[1] || { banner_url:''};
            $scope.headSources3 = $scope.headBannerSources[2] || { banner_url:''};
            $scope.headSources4 = $scope.headBannerSources[3] || { banner_url:''};
            $scope.headSources5 = $scope.headBannerSources[4] || { banner_url:''};
        });


        $scope.saveHeadSources = function (headSource) {
            if(!headSource.banner_url){
                logger.logError('图片地址为空');
                return ;
            }
            bannerService.saveAllBannerSources(headSource).then(function(result){
                if(result && result.code == 0 ){
                    logger.logSuccess((result && result.message) || '更新成功');
                }
            })
        };

        $scope.imgUploadCallback = function () {
            $scope.$apply();
        };


}]).factory('bannerService', ['$http', function ($http) {
    return {
        getHeadBannerSources: function () {
            return $http({
                    method: 'GET',
                    url: "/banners/headBanner"
                }
            ).then(function (result) {
                return result.data;
            })
        }
        ,
        saveAllBannerSources: function (sources) {
            return $http({
                    method: 'put',
                    url: "/banners/headBanner",
                    data: sources
                }
            ).then(function (result) {
                return result.data;
            })
        }

    }}])